import validate from '../../libs/validate';
import headerImgUploader from "../Components/headerImgUploader";

export default {
    components:{
        headerImgUploader
    },
    computed: {
        user() {
            return this.$store.state.user.data
        }
    },
    data() {
        return {
            searchKey: '',
            pageCount: 0,
            pageNum: 1,
            pageLoading: false,
            showModal: false,
            appLauncherList:[],
            modalLoading:'',
            selectAppLauncher:'',
            uploadImgCallbackPath:'',
            uploadImgData:{},
            appLauncherForm:{
                resolution_ratio:'',
                link_type_code:'',
                link_url:'',
                link_detail_id:'',
            },
            appLauncherFormRules:{
                resolution_ratio:[{
                    required: true, message: '请输入分辨率', trigger: 'blur'
                }],
                link_type_code:[{
                    required: true, message: '请选择一个类型', trigger: 'change'
                }],
                link_url: [{
                        required: false,
                        validator: validate.can_empty_url,
                        trigger: 'blur'
                    }]

            },
            linkDetailTypeList:[]
        }
    },
    methods: {
        /**
         * @param {Boolean} type
         * 设置确定按钮loading动画，传True或者False
         */
        changeLoadingStatus(type) {
            if (!type) {
                this.modalLoading = false;
            }
            this.$nextTick(() => {
                this.modalLoading = true;
            });
        },
        // doSearch() {
        //     this.pageNum = 1;
        //     this.getLauncherList()
        // },
        // clearSearch() {
        //     this.searchKey = '';
        //     this.pageNum = 1;
        //     this.getLauncherList()
        //
        // },
        changePage(num) {
            this.pageNum = num;
            this.getLauncherList()
            document.querySelector('.single-page-con').scrollTo(0, 0)
        },
        getLauncherList() {
            if (this.pageLoading) return
            this.pageLoading = true;
            this.http.post(this.postUrl.doGetAppLauncherList, {
                token: this.user.tokenList[0],
                pageNum: this.pageNum,
                pageSize: 20,
                key_word: this.searchKey
            }).then((data) => {
                console.log(data);
                if (data.code > -1) {
                    this.pageCount = data.data.totalRow;
                    this.appLauncherList = JSON.parse(JSON.stringify(data.data.list));
                } else {
                    this.errorHandle(data.msg)
                }
            }, () => {
                this.errorHandle()
            }).then(() => {
                this.pageLoading = false;
            })
        },
        clickOK(){
            this.changeLoadingStatus(true)
            if (!this.uploadImgCallbackPath) {
                this.changeLoadingStatus(false);
                this.errorHandle("请先上传图片！");
                return;
            }
            this.$refs.appLauncherForm.validate(valid=>{
                if(!valid){
                    this.changeLoadingStatus(false);
                    return
                }
            })
            let postData = JSON.parse(JSON.stringify(this.appLauncherForm));
            postData.token = this.user.tokenList[0];
            postData.image_url = this.uploadImgCallbackPath;
            postData.link_url = encodeURIComponent(postData.link_url)

            if(this.selectAppLauncher){
                postData.uuid = this.selectAppLauncher.uuid;
            }
            delete postData.isShow
            this.http.post(this.postUrl.doAddLauncherUrl,postData).then((data)=>{
                if(data.code > -1){
                    this.$Notice.success({
                        title: this.selectAppLauncher?'修改成功！':'新增成功！'
                    })
                    this.showModal = false;
                    this.getLauncherList();
                }else{
                    this.errorHandle(data.msg)
                }
                this.changeLoadingStatus(false)

            },()=>{
                this.errorHandle()
                this.changeLoadingStatus(false)

            })
        },
        editLauncher(launcher){
            this.selectAppLauncher = launcher;
            console.log(launcher);
            this.uploadImgCallbackPath = launcher.image_url;
            this.appLauncherForm =  {
                resolution_ratio:this.selectAppLauncher.resolution_ratio,
                link_type_code:this.selectAppLauncher.link_type_code,
                link_url:this.selectAppLauncher.link_url,
                link_detail_id:this.selectAppLauncher.link_detail_id,
            }
            this.showModal = true;
        },
        uploadImgSuccess(data) {
            this.uploadImgCallbackPath = data;
        },
        createImgUploadData(){
            //上传图片
            let uploadImgData = {
                token: this.user.tokenList[0],
                type: 1,
                app_key: this.http.app_key,
                timestamp: "2017-09-26 15:52:28"
            }
            let uploadImgDataSign = this.http.getSign({
                type: 1,
                token: this.user.tokenList[0]
            });
            uploadImgData.sign = uploadImgDataSign;
            this.uploadImgData = uploadImgData;
        },
        linkTypeCodeChange(type){
            if(type == 'WEB' || !this.showModal) return
                this.appLauncherForm.link_url = '';
            this.http.post(this.postUrl.getBannerJumpTypeUrl,{
                token:this.user.tokenList[0],
                link_type_code:type
            }).then((data)=>{
                console.log(data);
                if(data.code > -1){
                    this.linkDetailTypeList = data.data
                }else{
                    this.errorHandle('类型查询失败')
                }
            },()=>{
                this.errorHandle('网络错误，类型查询失败')
            })
        },
        deleteLauncher(banner){
            this.$Modal.confirm({
                    title: '提示',
                    loading: true,
                    content: '<p>确定要删除这个启动图?</p>',
                    onOk: () => {
                        this.http.post(this.postUrl.doDeleteAppLauncher,{
                            token:this.user.tokenList[0],
                            uuid:banner.uuid
                        }).then((data)=>{
                            if(data.code > -1){
                                this.$Notice.success({
                                    title: '操作成功！',
                                })
                                if(this.appLauncherList.length === 1){
                                    this.pageNum = 1;
                                }
                                this.getLauncherList()
                            }else {
                                this.errorHandle(data.msg)
                            }
                        }, () => {
                            this.errorHandle()
                        }).then(()=>{
                            this.$Modal.remove();
                        })
                    },
                });
        }
    },
    created() {
        if (this.user) {
            this.getLauncherList()
            this.createImgUploadData()
        }

    },
    watch: {
        user(val, old) {
            if (val && !old) {
                this.getLauncherList()
                this.createImgUploadData()


            }
        },
        showModal(val){
            if(!val){
                this.selectAppLauncher = '';
                setTimeout(()=>{
                    this.uploadImgCallbackPath = "";
                    this.appLauncherForm={
                        resolution_ratio:'',
                        link_type_code:'',
                        link_url:'',
                        link_detail_id:'',
                    }

                },300)
                this.$refs.appLauncherForm.resetFields()

            }
        }
    }
}
